from fastapi import FastAPI,Cookie
from pydantic import BaseModel
from typing import Annotated

app = FastAPI()

# 显性声明cookie参数
@app.get("/items1/")
async def read_items(ads_id: Annotated[str | None, Cookie()] = None):
    return {"ads_id": ads_id}

"""
带有 Pydantic 模型的 Cookie¶
在 Pydantic 模型中声明所需的 cookie 参数，然后将参数声明为 Cookie ：
"""
class Cookies(BaseModel):
    model_config = {"extra": "forbid"} #出现额外字段就拒绝
    session_id: str
    fatebook_tracker: str | None = None
    googall_tracker: str | None = None


@app.get("/items2/")
async def read_items(cookies: Annotated[Cookies, Cookie()]):
    return cookies